Objects and Data Structures Assessment Test

Test your knowledge.

Answer the following questions

Write a brief description of all the following Object Types and Data Structures we've learned about:

Numbers:

Strings:

Lists:

Tuples:

Dictionaries:

Numbers

Write an equation that uses multiplication, division, an exponent, addition, and subtraction that is equal to 100.25.

Hint: This is just to test your memory of the basic arithmetic commands, work backwards from 100.25


In [2]:
print 10*100/10+5.75-5.5


100.25

Explain what the cell below will produce and why. Can you change it so the answer is correct?


In [4]:
2.0/3


Out[4]:
0.6666666666666666

Answer these 3 questions without typing code. Then type code to check your answer.

What is the value of the expression 4 * (6 + 5)

What is the value of the expression 4 * 6 + 5 

What is the value of the expression 4 + 6 * 5 

In [7]:
print 4*(6+5)
print 4*6+5
print 4+6*5
print 3+1.5+4


44
29
34
8.5

What is the type of the result of the expression 3 + 1.5 + 4?

What would you use to find a number’s square root, as well as its square?


In [9]:
print 2**(0.5)


1.41421356237

Strings

Given the string 'hello' give an index command that returns 'e'. Use the code below:


In [10]:
s = 'hello'
# Print out 'e' using indexing
print s[1]

# Code here


e

Reverse the string 'hello' using indexing:


In [17]:
s ='hello'

# Reverse the string using indexing
print s[::-1]
print s[:3:-1]
# Code here


olleh
o

Given the string hello, give two methods of producing the letter 'o' using indexing.


In [18]:
s ='hello'

# Print out the
print s[4]
print s[-1]

# Code here


o
o

Lists

Build this list [0,0,0] two separate ways.


In [24]:
a = list([0,0,0])
print a
a = list([0,0])
print a
a.append(0)
print a


[0, 0, 0]
[0, 0]
[0, 0, 0]

Reassign 'hello' in this nested list to say 'goodbye' item in this list:


In [26]:
l = [1,2,[3,4,'hello']]
l[2][2] = 'goodbye'
print l


[1, 2, [3, 4, 'goodbye']]

Sort the list below:


In [48]:
l = [3,4,5,5,6,1]
print l
l.sort()
print l
l = [3,4,5,5,6,1]
print sorted(l)
print l


[3, 4, 5, 5, 6, 1]
[1, 3, 4, 5, 5, 6]
[1, 3, 4, 5, 5, 6]
[3, 4, 5, 5, 6, 1]

Dictionaries

Using keys and indexing, grab the 'hello' from the following dictionaries:


In [31]:
d = {'simple_key':'hello'}
# Grab 'hello'
print d['simple_key']


hello

In [32]:
d = {'k1':{'k2':'hello'}}
# Grab 'hello'
print d['k1']['k2']


hello

In [39]:
# Getting a little tricker
d = {'k1':[{'nest_key':['this is deep',['hello']]}]}

# Grab hello
print d['k1'][0]['nest_key'][1][0]


hello

In [43]:
# This will be hard and annoying!
d = {'k1':[1,2,{'k2':['this is tricky',{'tough':[1,2,['hello']]}]}]}

print d['k1'][2]['k2'][1]['tough'][2][0]


hello

Can you sort a dictionary? Why or why not?

Not sure about this.

Tuples

What is the major difference between tuples and lists?

How do you create a tuple?

Sets

What is unique about a set?

Use a set to find the unique values of the list below:


In [49]:
l = [1,2,2,33,4,4,11,22,3,3,2]
set(l)


Out[49]:
{1, 2, 3, 4, 11, 22, 33}

Booleans

For the following quiz questions, we will get a preview of comparison operators:

OperatorDescriptionExample
== If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!= If values of two operands are not equal, then condition becomes true.
<> If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator.
> If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true.
< If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true.
>= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true.
<= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.

What will be the resulting Boolean of the following pieces of code (answer fist then check by typing it in!)


In [50]:
# Answer before running cell
2 > 3


Out[50]:
False

In [51]:
# Answer before running cell
3 <= 2


Out[51]:
False

In [52]:
# Answer before running cell
3 == 2.0


Out[52]:
False

In [53]:
# Answer before running cell
3.0 == 3


Out[53]:
True

In [54]:
# Answer before running cell
4**0.5 != 2


Out[54]:
False

Final Question: What is the boolean output of the cell block below?


In [55]:
# two nested lists
l_one = [1,2,[3,4]]
l_two = [1,2,{'k1':4}]

#True or False?
l_one[2][0] >= l_two[2]['k1']


Out[55]:
False

Great Job on your first assessment!